/*
 * Copyright 2009 Biz-e (info@biz-e.nl)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.acl.client;

import nl.biz_e.acl.client.ui.ACLPanel;
import nl.biz_e.acl.client.ui.ContextPanel;
import nl.biz_e.acl.client.ui.UserPanel;
import nl.biz_e.acl.shared.model.User;
import nl.biz_e.aclclient.shared.AclAuthenticator;
import nl.biz_e.gwt.baselib.ui.client.menu.IMenuItem;
import nl.biz_e.gwt.baselib.ui.client.menu.MenuItem;
import nl.biz_e.gwt.baselib.ui.client.menu.IMenuItem.MenuItemClickedListener;
import nl.biz_e.gwtappengine.lib.client.AbstractApplication;

/**
 * Main application class for the ACL system.<br/>
 * 
 */
public class ACL extends AbstractApplication {
	private IMenuItem mUser;
	private IMenuItem mCtx;
	private UserPanel userPanel;
	private ContextPanel ctxPanel;
	
	/**
	 * Constructs a new ACL instance
	 */
	public ACL() {
		super("Access Control List");
		this.setShowPreLoginPanel(false);
		// setUseGoogleLogin(true);
		setUseGoogleLogin(true, "/ACL.html");
		this.setAuthenticator(new AclAuthenticator());
		
		MenuItemClickedListener menuListener = new MenuItemClickedListener() {
			@Override
			public void menuItemClicked(IMenuItem item) {
				item.getManager().setOnlySelectedMenuItem(item);
				if (item == mUser) {
					showPanel(getUserPanel());
				} else if (item == mCtx) {
					showPanel(getContextPanel());
				}
			}
		};
		
		mUser = new MenuItem("User(groups)", menuListener, false);
		mCtx = new MenuItem("Contexts", menuListener, false);
		
		this.addMenuItem(mUser);
		this.addMenuItem(mCtx);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void onLoggedIn() {
		showPanel(new ACLPanel());
		User user = new User();
		user.setUserId("test@gmail.com");
		user.setName("Test User");
		// GenericService.save(user);
	}
	
	private UserPanel getUserPanel() {
		if (userPanel == null) {
			userPanel = new UserPanel();
		}
		return userPanel;
	}
	
	private ContextPanel getContextPanel() {
		if (ctxPanel == null) {
			ctxPanel = new ContextPanel();
		}
		return ctxPanel;
	}
}
